package com.gua.jbs.daoImpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gua.jbs.controller.LoginController;
import com.gua.jbs.dao.IBaseDao;
import com.gua.jbs.utility.QueryConstant;

public class BaseDao implements IBaseDao {

	private static final Logger logger = Logger.getLogger(BaseDao.class);
	private Connection connection = null;

	/**
	 * Method to get the DB Connection
	 */
	public Connection getConnection() throws SQLException {

		try {
			String ConnectionURL = URL + DATABASE;
			Class.forName(DRIVER_NAME);
			if (connection == null)
				connection = DriverManager.getConnection(ConnectionURL,
						USER_NAME, PASSWORD);
		} catch (SQLException sqe) {
			logger.error(sqe.getMessage());
			throw sqe;
		} catch (ClassNotFoundException cnfe) {
			logger.error(cnfe.getMessage());
		}
		return connection;
	}

	/**
	 * Method to get the Statement object
	 */
	public Statement getStmt() throws SQLException {
		Statement stmt = null;
		try {
			connection = getConnection();
			stmt = connection.createStatement();
		} catch (SQLException sqe) {
			logger.error(sqe.getMessage());
			throw sqe;
		}
		return stmt;
	}

	/**
	 * Method to get the Query results
	 * 
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public ResultSet getResults(String query) throws SQLException {
		ResultSet rs = null;
		Statement stmt = getStmt();
		try {
			if (stmt != null)
				rs = stmt.executeQuery(query);
		} catch (SQLException sqe) {
			logger.error(sqe.getMessage());
			throw sqe;
		}
		return rs;
	}

	/**
	 * Method to save data in database
	 */
	public int insert(String query) throws SQLException {
		int result = 0;
		Statement stmt = getStmt();
		try {
			if (stmt != null)
				result = stmt.executeUpdate(query);
		} catch (SQLException sqe) {
			logger.error(sqe.getMessage());
			throw sqe;
		}
		return result;
	}

	/**
	 * Method to update data in database
	 */
	public int update(String query) throws SQLException {
		int result = 0;
		Statement stmt = getStmt();
		try {
			if (stmt != null)
				result = stmt.executeUpdate(query);
		} catch (SQLException sqe) {
			logger.error(sqe.getMessage());
			throw sqe;
		}
		return result;
	}

	/**
	 * Method to delete record from the database
	 */
	public int delete(String query) throws SQLException {
		int result = 0;
		Statement stmt = getStmt();
		try {
			if (stmt != null)
				result = stmt.executeUpdate(query);
		} catch (SQLException sqe) {
			logger.error(sqe.getMessage());
			throw sqe;
		}
		return result;
	}

	/**********************************************************************************************************
	 * @description :Replaces the parameters in query, from paramList. the
	 *              parameters are identified on basis of the paramList index.
	 * @param :query
	 * @return : String
	 **********************************************************************************************************/
	public String getQuery(String query, List paramList) {
		for (int IntI = 0; IntI < paramList.size(); IntI++) {
			query = query
					.replaceAll(":" + IntI + ":", "" + paramList.get(IntI));
		}
		return query;
	}

	/**
	 * Get the primary key value for the table
	 * 
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public int getPrimaryKey(String fieldName, String tableName)
			throws SQLException {
		String query = "select MAX(" + fieldName + ") AS maxId FROM "
				+ tableName;
		ResultSet rs = null;
		int maxPrimary = 0;
		try {
			rs = getResults(query);
			if (rs != null && rs.next())
				maxPrimary = rs.getInt(1);
		} catch (SQLException sqe) {
			logger.error(sqe.getMessage());
			throw sqe;
		}
		return maxPrimary + 1;
	}

	public void closeConnection() throws SQLException {
		/*
		 * if (connection != null) { try { connection.close(); } catch
		 * (SQLException sqe) { logger.error(sqe.getMessage()); throw sqe; } }
		 */

	}
}
